"""
Adaptive Speech watermarking using the wavelet transform
"""
#
# wt.py
#
# Copyright (C) 2012 Robert Buj Gelonch
# Copyright (C) 2012 David Megias Jimenez
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
__author__ = "Robert Buj Gelonch, and David Megias Jimenez"
__copyright__ = "Copyright 2012, Robert Buj Gelonch and David Megias Jimenez"
__credits__ = ["Robert Buj Gelonch", "David Megias Jimenez"]
__license__ = "GPL"
__version__ = "3"
__maintainer__ = "Robert Buj"
__email__ = "rbuj@uoc.edu"
__status__ = "Development"
__docformat__ = 'plaintext'


from numpy import absolute
from numpy import array
from numpy import int16
from pywt import dwt
from pywt import idwt
from uoc.wave.watermark.message import watermark_message
from uoc.wave.watermark.schemes.generic import generic_detection_function as D_generic

def embedding_function(S, w, P_watermarking_sheme):
    """
    embedding_function
    """
    # Apply the wavelet transform in the original audio signal
    cA, cD = dwt(S, P_watermarking_sheme['numtaps'])
    # Get the watermark message
    m = watermark_message(w, P_watermarking_sheme['frame_size'], len(cA))
    # Add the watermark message on the cD component
    s = idwt(cA, m, P_watermarking_sheme['numtaps'])
    return array(s)

def retrieval_function(s, P_watermarking_sheme):
    """
    Return the embedded watermark message within the watermarked
    audio signal
    """
    # Apply the wavelet transform in the original audio signal
    cA, cD = dwt(s, P_watermarking_sheme['numtaps'])
    R = (absolute(cD) >= 0.9999).astype(int16)
    return R

def detection_function(s, w, P_watermarking_sheme):
    """
    Evaluate whether the watermarked signal contains the water-
    marked message
    """
    return D_generic(s, w, P_watermarking_sheme)

